# coding=utf-8
class ConfigHelper(object):

    def __init__(self):
        self._isop = False
        """:type: bool"""
        self._configs = []
        """:type: list[str]"""
        self._config2options = {}
        """:type: dict[str, list[dict]]"""

        self._selected = -1
        """:type: int"""
        self._namespace = ''
        """:type: str"""
        self._options = []
        """:type: list[dict]"""

    def merge(self, info, server):
        """
        :type info: dict
        :type server: bool
        """
        configs = info['configs']
        """:type: list[str]"""
        sequenceMapping = info['sequenceMapping']
        """:type: dict[str, list[str]]"""
        contentMapping = info['contentMapping']
        """:type: dict[str, dict[str, dict]]"""
        operatorMapping = info['operatorMapping']
        """:type: dict[str, dict[str, bool]]"""
        # configs
        old = set(self._configs)
        self._configs.extend(i for i in configs if i not in old)
        # options
        for namespace, sequence in sequenceMapping.items():
            content = contentMapping[namespace]
            operator = operatorMapping[namespace]
            options = self._config2options.setdefault(namespace, [])
            for key in sequence:
                option = content[key].copy()
                option['key'] = key
                option['operator'] = operator.get(key)
                option['server'] = server
                options.append(option)

    def clear(self):
        self._isop = False
        self._configs = []
        self._config2options = {}

        self._namespace = ''
        self._options = []

    @property
    def isop(self):
        """
        :rtype: bool
        """
        return self._isop

    @isop.setter
    def isop(self, value):
        """
        :type value: bool
        """
        self._isop = value

    @property
    def configs(self):
        """
        :rtype: list[str]
        """
        return self._configs

    @property
    def selected(self):
        """
        :rtype: int
        """
        return self._selected

    @selected.setter
    def selected(self, value):
        """
        :type value: int
        """
        if 0 <= value < len(self._configs):
            self._selected = value
            self._namespace = self._configs[self._selected]
            self._options = self._config2options[self._namespace]
        else:
            self._selected = -1
            self._namespace = ''
            self._options = []

    @property
    def namespace(self):
        """
        :rtype: str
        """
        return self._namespace

    @property
    def options(self):
        """
        :rtype: list[dict]
        """
        return self._options
